package com.wuhan.template.module.find.webview;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.module.find.bean.CollectionEntity;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.onekeyshare.OnekeyShare;
import greendao.DaoMaster;
import greendao.DaoSession;
import greendao.DetailTable;


public class WebViewActivity extends BaseActivity {

    @BindView(R.id.web)
    WebView mWebView;
    @BindView(R.id.web_title)
    TextView mWebTitle;
    String url;
    String title;
    private WindowManager.LayoutParams wmParams;
    private PopupWindow mPopupWindow;
    private long time;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview_layout;
    }

    @Override
    protected void loadData() {
        Intent intent = getIntent();
        url = (String) intent.getCharSequenceExtra("url");
        title = (String) intent.getCharSequenceExtra("title");
        mWebTitle.setText(title);
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
    }

    @OnClick(R.id.web_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.share)
    public void share() {
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        oks.setTitle(url);
        oks.setText("掌上武汉app");
        oks.setUrl(url);
        oks.setComment("我是测试评论文本");
        oks.setSite(getString(R.string.app_name));
        oks.setSiteUrl(url);
        oks.show(this);

}

    @OnClick(R.id.collect)
    public void collect() {

        time = System.currentTimeMillis();
       /* DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"collect_db",null);
        SQLiteDatabase db = helper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        NoteDao noteDao = daoSession.getNoteTableDao();*/
        //在BaseApplication里面已经初始化
        addNote();

    }

    private void addNote() {
        // 插入操作，简单到只要你创建一个 Java 对象
        DetailTable entity = new DetailTable(null,url,title,time);
        DaoSession daoSession = ((BaseApplication) getApplication()).getDaoSession();
        daoSession.getDetailTableDao().insert(entity);
        Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed()
    {
        // 判断是否为空，并且显示了
        if (mPopupWindow != null && mPopupWindow.isShowing())
        {
            // 取消mPopupWindow显示
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
        // finish activity
        super.onBackPressed();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        // 如果按返回键
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            // 判断是否为空，并且显示了
            if (mPopupWindow != null && mPopupWindow.isShowing())
            {
                // 取消mPopupWindow显示
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.comment)
    public void comment() {
    }

}
